package prictise.com.application1.fullScreen;

import android.graphics.Rect;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.view.DisplayCutout;
import android.view.View;
import android.view.Window;
import android.view.WindowInsets;
import android.view.WindowManager;

import java.util.List;

import prictise.com.application1.BaseActivity;
import prictise.com.application1.R;

public class FullScreenActivity extends BaseActivity {
  private static final String TAG = "FullScreenActivity";

  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //取消标题栏
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    //取消状态栏
     this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_full_screen);

    //全屏显示
    getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
    WindowManager.LayoutParams lp = getWindow().getAttributes();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
    //下面图1
//      lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER;
      //下面图2
        lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
      //下面图3
//        lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT;
    }

    boolean notch = getPackageManager().hasSystemFeature("ro.miui.notch");
    Log.d(TAG,"notch = " +notch);

    getWindow().setAttributes(lp);

    getNotchParams();
  }

  public void getNotchParams() {
    final View decorView = getWindow().getDecorView();

    decorView.post(new Runnable() {
      @Override
      public void run() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
          WindowInsets rootWindowInsets = decorView.getRootWindowInsets();
          if (rootWindowInsets == null) {
            Log.e("TAG", "rootWindowInsets为空了");
            return;
          }
          DisplayCutout displayCutout = rootWindowInsets.getDisplayCutout();
          if (displayCutout == null) {
            Log.e("TAG", "1 不是刘海屏");
            return;
          }
          Log.e("TAG", "安全区域距离屏幕左边的距离 SafeInsetLeft:" + displayCutout.getSafeInsetLeft());
          Log.e("TAG", "安全区域距离屏幕右部的距离 SafeInsetRight:" + displayCutout.getSafeInsetRight());
          Log.e("TAG", "安全区域距离屏幕顶部的距离 SafeInsetTop:" + displayCutout.getSafeInsetTop());
          Log.e("TAG", "安全区域距离屏幕底部的距离 SafeInsetBottom:" + displayCutout.getSafeInsetBottom());

          List<Rect> rects = displayCutout.getBoundingRects();
          if (rects == null || rects.size() == 0) {
            Log.e("TAG", "不是刘海屏");
          } else {
            Log.e("TAG", "刘海屏数量:" + rects.size());
            for (Rect rect : rects) {
              Log.e("TAG", "刘海屏区域：" + rect);
            }
          }
        }
      }
    });
  }
}
